package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;

import model.Device;
import control.MainListener;
import control.util.Util;

@SuppressWarnings("serial")
public class TreeGUI extends JPanel {
	
	private DefaultTreeCellRenderer treeRenderer;
	private DefaultTreeModel treeModel;
	private JTree treeDevice;
	private DefaultMutableTreeNode nodeDevices;
	private JScrollPane scrollTree;
	
	public TreeGUI() {
		initObjs();
		initProperties();
	}
	
	private void initObjs() {
		treeRenderer = new DefaultTreeCellRenderer();
		
		nodeDevices = new DefaultMutableTreeNode("Dispositivos");
		treeModel = new DefaultTreeModel(nodeDevices);
		treeDevice = new JTree(treeModel);
		
		scrollTree = new JScrollPane(treeDevice, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	}
	
	private void initProperties() {
		setLayout(new FlowLayout());
		setBackground(Color.WHITE);
		
		treeRenderer.setLeafIcon(new ImageIcon(Util.LEAF_ICON_PATH));
		treeRenderer.setTextSelectionColor(Util.COLOR_SELECTED_NODE_TEXT);
		treeRenderer.setTextNonSelectionColor(Util.COLOR_UNSELECTED_NODE_TEXT);
		
		treeDevice.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		treeDevice.setCellRenderer(treeRenderer);
		
		scrollTree.setPreferredSize(new Dimension(Util.TREE_PANEL_WIDTH, Util.MAIN_PANEL_HEIGHT));
		add(scrollTree);
	}
	
	public JTree getTreeDevice() {
		return treeDevice;
	}
	public DefaultMutableTreeNode getNodeDevices() {
		return nodeDevices;
	}
	public JScrollPane getScrollTree() {
		return scrollTree;
	}
	
	public void addListeners(MainListener listener) {
		treeDevice.addTreeSelectionListener(listener);
	}
	// ****************************** ADD DEVICE ******************************
	public void addNodeDevice(Device device) {
		DefaultMutableTreeNode nodeDevice = new DefaultMutableTreeNode(device);
		treeModel.insertNodeInto(nodeDevice, nodeDevices, nodeDevices.getChildCount());
		
		repaint();
	}
	
	
	public static void main(String[] args) {
		JFrame frame = new JFrame("Teste");
		final JPanel panel = new TreeGUI();
		frame.add(panel);
		// *********** PARA TESTE **********
		for (int i=0; i<10; i++) {
			Device device = new Device(i+"");
			device.addSystemName("dispositivo "+i);
			((TreeGUI)panel).addNodeDevice(device);
		}
		JButton addDevice = new JButton("Adicionar");
		addDevice.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Device device = new Device("11");
				device.addSystemName("novo dispositivo");
				((TreeGUI)panel).addNodeDevice(device);
			}
		});
		panel.add(addDevice);
		// **********************************
		frame.pack();
		frame.setPreferredSize(new Dimension(500, 500));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
}
